﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Sitecore.Data.Items;
using System.Data;

namespace Workflow.Alerts.Utils
{
    public static class Utils
    {
        public static string GetCurrentItem()
        {
            ItemAxes ia = new Sitecore.Data.Items.ItemAxes(Sitecore.Context.Item);
            Item[] iar = ia.GetAncestors();
            if (ia.Level <= 5) return Sitecore.Context.Item.Name;
            return iar[5].Name;
        }

        public static void PopulateDictionaries()
        {
            //We get all hashtables from sitecore and store them in the application memory
            //because they are shared between all users

            //Activity types
            Dictionary<int, string> activityTypes = new Dictionary<int, string>();
            Item activityTypesItem = Sitecore.Context.Database.GetItem("/sitecore/content/workflow/Definitions/Activity Type");
            if (activityTypesItem != null)
            {
                foreach (Item activityType in activityTypesItem.Children)
                {
                    activityTypes.Add(Int32.Parse(activityType.Fields["key"].Value),
                                        activityType.Fields["value"].Value);
                }
                HttpContext.Current.Application[WorkflowConstants.ActivityTypes] = activityTypes;
            }

            //Activity types
            Dictionary<int, string> alertTypes = new Dictionary<int, string>();
            Item alertTypesItem = Sitecore.Context.Database.GetItem("/sitecore/content/workflow/Definitions/Alert Type");
            if (alertTypesItem != null)
            {
                foreach (Item alertType in alertTypesItem.Children)
                {
                    alertTypes.Add(Int32.Parse(alertType.Fields["key"].Value),
                                        alertType.Fields["value"].Value);
                }
                HttpContext.Current.Application[WorkflowConstants.AlertTypes] = alertTypes;
            }
        }

        public static Dictionary<string, int> GetMenuStatuses(int? municipalityId, int? networkOperatorId)
        {
            var result = new Dictionary<string, int>();
            var tempData = new Dictionary<WorkflowEnums.Status, int>();
            UserDAL dal = new UserDAL();
            WorkflowFilter filter = new WorkflowFilter()
            {
                MunicipalityId = municipalityId,
                NetworkOperatorId = networkOperatorId
            };

            var dataResult = dal.GetMenuStatuses(new WorkflowFilterSql(filter));

            foreach (DataRow dr in dataResult.Rows)
            {
                tempData.Add((WorkflowEnums.Status)(int)dr["currentstatus"], (int)dr["num"]);
            }

            int? tempCount = null;
            if (municipalityId.HasValue)
            {
                if (tempData.Keys.Contains(WorkflowEnums.Status.Sent))
                    result[WorkflowConstants.NewAlerts.ToString()] = tempData[WorkflowEnums.Status.Sent];



                tempCount = null;
                if (tempData.Keys.Contains(WorkflowEnums.Status.Approved))
                    tempCount = tempData[WorkflowEnums.Status.Approved];
                if (tempData.Keys.Contains(WorkflowEnums.Status.PartiallyApproved))
                    tempCount = (tempCount ?? 0) + tempData[WorkflowEnums.Status.PartiallyApproved];
                if (tempData.Keys.Contains(WorkflowEnums.Status.Declined))
                    tempCount = (tempCount ?? 0) + tempData[WorkflowEnums.Status.Declined];

                if (tempCount.HasValue)
                    result[WorkflowConstants.InProgress.ToString()] = tempCount.Value;


                tempCount = null;
                if (tempData.Keys.Contains(WorkflowEnums.Status.Executed4Contractor))
                    tempCount = tempData[WorkflowEnums.Status.Executed4Contractor];
                if (tempData.Keys.Contains(WorkflowEnums.Status.Executed4Municipality))
                    tempCount = (tempCount ?? 0) + tempData[WorkflowEnums.Status.Executed4Municipality];
                if (tempCount.HasValue)
                    result[WorkflowConstants.Executed.ToString()] = tempCount.Value;

                tempCount = null;

                if (tempData.Keys.Contains(WorkflowEnums.Status.Accepted))
                    tempCount = tempData[WorkflowEnums.Status.Accepted];
                if (tempData.Keys.Contains(WorkflowEnums.Status.AcceptedPartially))
                    tempCount = (tempCount ?? 0) + tempData[WorkflowEnums.Status.AcceptedPartially];
                if (tempCount.HasValue)
                    result[WorkflowConstants.Invoices.ToString()] = tempCount.Value;


            }
            else
            {
                if (tempData.Keys.Contains(WorkflowEnums.Status.Created))
                    result[WorkflowConstants.Queued.ToString()] = tempData[WorkflowEnums.Status.Created];

                if (tempData.Keys.Contains(WorkflowEnums.Status.Sent))
                    result[WorkflowConstants.Sent.ToString()] = tempData[WorkflowEnums.Status.Sent];



                tempCount = null;
                if (tempData.Keys.Contains(WorkflowEnums.Status.Approved))
                    tempCount = tempData[WorkflowEnums.Status.Approved];
                if (tempData.Keys.Contains(WorkflowEnums.Status.PartiallyApproved))
                    tempCount = (tempCount ?? 0) + tempData[WorkflowEnums.Status.PartiallyApproved];
                if (tempData.Keys.Contains(WorkflowEnums.Status.Declined))
                    tempCount = (tempCount ?? 0) + tempData[WorkflowEnums.Status.Declined];

                if (tempCount.HasValue)
                    result[WorkflowConstants.InProgress.ToString()] = tempCount.Value;


                tempCount = null;
                if (tempData.Keys.Contains(WorkflowEnums.Status.Executed4Contractor))
                    tempCount = tempData[WorkflowEnums.Status.Executed4Contractor];
                if (tempData.Keys.Contains(WorkflowEnums.Status.Executed4Municipality))
                    tempCount = (tempCount ?? 0) + tempData[WorkflowEnums.Status.Executed4Municipality];
                if (tempData.Keys.Contains(WorkflowEnums.Status.Accepted))
                    tempCount = (tempCount ?? 0) + tempData[WorkflowEnums.Status.Accepted];
                if (tempData.Keys.Contains(WorkflowEnums.Status.AcceptedPartially))
                    tempCount = (tempCount ?? 0) + tempData[WorkflowEnums.Status.AcceptedPartially];
                if (tempCount.HasValue)
                    result[WorkflowConstants.Executed.ToString()] = tempCount.Value;
            }
            return result;
        }
    }
}
